•
Use the if statement to specify a
block of code to be executed if a condition is true.
Syntax
if (condition)
{
// block of code to be
executed if the condition is true
}
Example
class sample
{
public
static void main(String args[])
{
int
a=10, b=20;
if(a>b)
System.out.println(“A is greater than B”);
}
}
package Conditonal_Statements;
public class Simple_if {
// Java program to illustrate If statement
public static void main(String args[])
{
int i = 10;
if (i < 15)
{System.out.println("10 is less than 15");}
}
}
If…
else statement
•
Use the if statement to specify a
block of code to be executed if a condition is true.
•
Use the else statement to specify a
block of code to be executed if the condition is false.
Syntax
if (condition)
{
// block of code to be
executed if the condition is true
}
else
{
// block of code to be
executed if the condition is false
}
Example
– if…else
int a = 20;
if (a <18)
{
System.out.println("Good day.“);
}
else
{
System.out.println("Good evening.“);
}
Output:
Good evening
package Conditonal_Statements;
import java.util.Scanner;
public class if_else {
public static void main(String args[]) {
int year;
System.out.println("Enter Year : ");
Scanner in = new Scanner(System.in);
year = in.nextInt();
if (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)) {
System.out.println("Year " + year + " is a leap year");
} else {
System.out.println("Year " + year + " is not a leap year");
}
}
}
If…
else if statement
•
Use the else if statement to specify
a new condition if the first condition is false.
Syntax
if (condition1)
{
// block of code to be
executed if condition1 is true
}
else if (condition2)
{
// block of code to be
executed if the condition1 is false and condition2 is true
}
else
{
// block of code to be
executed if the condition1 is false and condition2 is false
}
Example
int a = 22;
if (a < 10)
{
System.out.println( "Good morning.“);
}
else if (a
< 20)
{
System.out.println( "Good day.“);
}
else
{
System.out.println( "Good evening.“);
}
Output
Good Evening
package Conditonal_Statements;
import java.util.Scanner;
public class Nested_if {
public static void main(String args[])
{
/*
Nested if Statement
A company insures its drivers in the following cases:
a. If the driver is married.
b. If the driver is unmarried, male & above 30 years of age.
c. If the driver is unmarried, female & above 25 years of age.
*/
Scanner in =new Scanner(System.in);
System.out.println("Enter The Marital Status M/U: ");
char marital=in.next().charAt(0);
if(marital=='u' || marital=='U' )
{
System.out.println("Enter The Gender M/F: ");
char gender=in.next().charAt(0);
System.out.println("Enter The Age : ");
int age=in.nextInt();
if((gender=='M'||gender=='m')&& age>=30)
{
System.out.println("You are Eligible for Insurance");
}
else if((gender=='F'||gender=='f')&& age>=25)
{
System.out.println("You are Eligible for Insurance");
}
else
{
System.out.println("You are Not Eligible for Insurance");
}
}
else if(marital=='m' || marital=='M' )
{
System.out.println("You are Eligible for Insurance");
}
else {
System.out.println("Invalid Input");
}
}
}
Switch
Statement
Use the switch statement to
select one of many code blocks to be executed.
Syntax:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This
is how it works:
•
The switch expression is
evaluated once
•
The value of the expression is compared
with the values of each case
•
If there is a match, the associated
block of code is executed
Example:
int day
= 4;
switch (day) {
case 1:
System.out.println( "Monday“);
break;
case 2:
System.out.println("Tuesday“);
break;
case 3:
System.out.println( "Wednesday“);
break;
case 4:
System.out.println( "Thursday“);
break;
case 5:
System.out.println( "Friday“);
break;
case 6:
System.out.println( "Saturday“);
break;
case 7:
System.out.println( "Sunday“);
break;
default:
System.out.println(“Invalid Day”);
}
Output: Thursday
package Conditonal_Statements;
import java.util.Scanner;
//Switch Case Statement in Java
public class switch_demo {
public static void main(String args[]) {
int a,b,c,ch;
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println("Enter Your Choice : ");
Scanner in =new Scanner(System.in);
ch=in.nextInt();
System.out.println("Enter 2 Nos : ");
a=in.nextInt();
b=in.nextInt();
switch (ch)
{
case 1:
c=a+b;
System.out.println("Addition : " +c);
break;
case 2:
c=a-b;
System.out.println("Subtraction : "+c);
break;
case 3:
c=a*b;
System.out.println("Multiplication : "+c);
break;
case 4:
c=a/b;
System.out.println("Division : "+c);
break;
default:
System.out.println("Invalid Selection");
break;
}
}
}
0 Comments